﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ajayumi.develop.win32.Transport.Utils
{
    /// <summary>
    /// 文件处理辅助类
    /// Author  : 黄乙冬
    /// Date    : 2011-08-28
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// 将文件大小格式化
        /// </summary>
        /// <param name="fileSize"></param>
        /// <returns></returns>
        public static string FormatFileSize(long fileSize)
        {
            string result = string.Empty;

            if (fileSize <= 0)
            { result = "0 MB"; }
            else
            { result = string.Format("{0:####0.00} MB", ((double)fileSize) / (1024 * 1024)); }

            //if (fileSize <= 0)
            //{ return "0 MB"; }
            //else if (fileSize >= (1024 * 1024 * 1024))
            //{ result = string.Format("{0:########0.00} GB", ((double)fileSize) / (1024 * 1024 * 1024)); }
            //else if (fileSize >= (1024 * 1024))
            //{ result = string.Format("{0:####0.00} MB", ((double)fileSize) / (1024 * 1024)); }
            //else if (fileSize >= 1024)
            //{ result = string.Format("{0:####0.00} KB", ((double)fileSize) / 1024); }
            //else
            //{ result = string.Format("{0} bytes", fileSize); }

            return result;
        }

        /// <summary>
        /// 获取给定目录下的所有切片
        /// </summary>
        /// <param name="dirPathes"></param>
        /// <returns></returns>        
        public static string[] GetSubFiles(params string[] dirPathes)
        {
            List<string> fileList = new List<string>();
            foreach (string url in dirPathes)
            {
                if (Directory.Exists(url))
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(url);
                    if (dirInfo.Exists)
                    {
                        RecursiveGetSubFiles(dirInfo, fileList);
                    }
                }
            }

            return fileList.ToArray();
        }

        /// <summary>
        /// 递归获取
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        private static void RecursiveGetSubFiles(DirectoryInfo dir, List<string> fileList)
        {
            try
            {
                foreach (FileInfo item in dir.GetFiles())
                {
                    fileList.Add(item.FullName);
                }

                DirectoryInfo[] subDirs = dir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                {
                    RecursiveGetSubFiles(subDir, fileList);
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// 根据文件完整路径，创建文件夹目录
        /// </summary>
        /// <param name="path"></param>
        public static void CreateDir(string path)
        {
            string pathRoot = Path.GetPathRoot(path);
            string directory = Path.GetDirectoryName(path);
            string[] dirs = directory.Replace(pathRoot, string.Empty).Split('\\');

            string dir = pathRoot;
            foreach (string item in dirs)
            {
                dir = Path.Combine(dir, item);
                if (!Directory.Exists(dir))
                { Directory.CreateDirectory(dir); }
            }
        }

        ///// <summary>
        ///// 移动文件夹下文件
        ///// </summary>
        ///// <param name="sourceDir"></param>
        ///// <param name="targetDir"></param>
        //public static void MoveFiles(string sourceDir, string targetDir, bool isRecursive)
        //{
        //    if (Directory.Exists(sourceDir))
        //    {
        //        string[] files = Directory.GetFiles(sourceDir);
        //        if (isRecursive)
        //        {
        //        }
        //        else
        //        {
        //            foreach (string item in files)
        //            {
                      
        //            }
        //        }
        //    }
        //}
    }
}
